<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置<small>用户管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="用户名/手机号" v-model="pagination.queryString" clearable style="width: 200px;"
                          class="filter-item" @input="handleCurrentChange(1)"></el-input>
                <el-button icon="el-icon-search" @click="handleCurrentChange(1)" class="dalfBut">查询</el-button>
                <el-button type="primary" class="el-icon-plus" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="birthday" label="出生日期" :formatter="dateFormat" align="center"></el-table-column>


                <el-table-column prop="username" label="用户名" align="center"></el-table-column>
                <el-table-column prop="telephone" label="手机号" align="center"></el-table-column>

                <el-table-column label="适用性别" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.gender == '0' ? '女':'男'}}</span>
                    </template>
                </el-table-column>

                <el-table-column prop="station" label="状态" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.station == '0' ? '关闭' : '开启'}}</span>
                        <el-switch
                                v-model="scope.row.station"
                                active-color="#13ce66"
                                inactive-color="#ff4949"
                                on-text="开启"
                                off-text="关闭"
                                @change="handleStation(scope.row)"
                                active-value="1"
                                inactive-value="0">
                        </el-switch>
                    </template>
                </el-table-column>

                <el-table-column prop="remark" label="说明" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" class="el-icon-edit-outline"
                                   @click="handleUpdate(scope.row)">编辑
                        </el-button>
                        <el-button size="mini" type="danger" class="el-icon-delete" @click="handleDelete(scope.row)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增用户" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px" ref="dataAddForm" :model="formData"
                                         :rules="rules">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户名" prop="username">
                                                <el-input v-model="formData.username"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="密码"  prop="password">
                                                <el-input v-model="formData.password"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="适用性别">
                                                <el-select v-model="formData.gender">
                                                    <el-option label="女" :value="0"></el-option>
                                                    <el-option label="男" :value="1"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="状态" prop="station">
                                                <el-select v-model="formData.station">
                                                    <el-option label="关闭" :value="0"></el-option>
                                                    <el-option label="开启" :value="1"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="手机号" prop="telephone">
                                                <el-input v-model="formData.telephone"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="出生日期">
                                                <!--<el-input v-model="formData.birthday" type="textarea"></el-input>-->
                                                <el-date-picker
                                                        v-model="formData.birthday"
                                                        type="date"
                                                        placeholder="选择日期">
                                                </el-date-picker>
                                            </el-form-item>

                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="说明" prop="remark">
                                                <el-input v-model="formData.remark" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>

                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名称</th>
                                            <th>关键字</th>
                                            <th>角色说明</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑用户" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px" ref="dataEditForm" :model="formData"
                                         :rules="rules2">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户名" prop="username">
                                                <el-input v-model="formData.username"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="密码"  prop="password">
                                                <el-input  v-model="formData.password" placeholder="请输入要修改的密码(空为不修改)"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="适用性别">
                                                <el-select v-model="formData.gender">
                                                    <el-option label="女" value="0"></el-option>
                                                    <el-option label="男" value="1"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="状态" prop="station">
                                                <el-select v-model="formData.station">
                                                    <el-option label="关闭" value="0"></el-option>
                                                    <el-option label="开启" value="1"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="手机号" prop="telephone">
                                                <el-input v-model="formData.telephone"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="出生日期">
                                                <el-date-picker
                                                        v-model="formData.birthday"
                                                        type="date"
                                                        placeholder="选择日期">
                                                </el-date-picker>
                                            </el-form-item>

                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="说明" prop="remark">
                                                <el-input v-model="formData.remark" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名称</th>
                                            <th>关键字</th>
                                            <th>角色说明</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script src="../plugins/common.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            user_name:'',
            activeName:'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            station_options: [{
                value: '1',
                label: '开启'
            }, {
                value: '0',
                label: '关闭'
            }],

            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            tableData: [],//新增/编辑回显数据
            roleIds: [],
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则-第1套,适用新建
                password: [{required: true, message: '密码为必填项', trigger: 'blur'}],
                username: [{required: true, message: '用户名为必填项', trigger: 'blur'}],
                telephone:[{required: true, message: '手机号为必填项', trigger: 'blur'}]
            },
            rules2:{//校验规则-第2套,适用编辑
                username: [{required: true, message: '用户名为必填项', trigger: 'blur'}]
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();//VUE对象初始化完成后调用分页查询方法，完成分页查询
            this.user_name = JSON.parse(parent.localStorage.getItem("username"));
        },
        methods: {
            handleStation(row){
                if (row.username==='admin'){
                    this.$message.error('超级管理员为最高权限,不可禁用');
                    row.station='1';
                    return ;
                }
                if(this.user_name===row.username){
                    this.$message.error('禁用用户失败,不可禁用自身');
                    row.station='1';
                    return ;
                }
                // alert(row.id+row.station+row.telephone+row.username+row.password);
                axios.post("/user/updateStation.do?id=" + row.id+"&station="+row.station).then(response => {
                    if (response.data.flag) {
                        this.$message.success(response.data.message);
                        return;
                    }
                    this.$message.error(response.data.message);
                }).catch((error) => {
                    this.showMessage(error);
                });
            },
            //编辑
            handleEdit() {
                if (this.formData.password.length<6&&this.formData.password.length>0){
                    this.$message.error('密码长度不符合要求(需大于等于6位)');
                    return;
                }
                this.$refs['dataEditForm'].validate(valid => {
                    if (valid) {
                        axios.post("/user/update.do?roleIds=" + this.roleIds, this.formData).then(response => {
                            if (response.data.flag) {
                                //隐藏弹出框
                                this.dialogFormVisible4Edit = false;
                                this.$message.success(response.data.message);
                                return;
                            }
                            this.$message.error(response.data.message);
                        }).catch((error) => {
                            this.showMessage(error);
                        }).finally(() => {
                            this.findPage();
                        })
                    } else {
                        //表单校验不通过
                    }
                });
            },

            //时间格式化
            dateFormat: function (row, column) {
                if (row.birthday != null) {
                    var t = new Date(row.birthday);//row 表示一行数据, updateTime 表示要格式化的字段名称
                    return t.getFullYear() + "-" + (t.getMonth() + 1) + "-" + t.getDate();
                }
            },
            //添加
            handleAdd() {
                if(this.formData.telephone.length!=11){
                    this.$message.error('手机号长度不符合要求(需要11位)');
                    return;
                }
                if (this.formData.password.length<6){
                    this.$message.error('密码长度不符合要求(需大于等于6位)');
                    return;
                }
                //添加表单校验，检查项不能为空
                this.$refs['dataAddForm'].validate(valid => {
                    if (valid) {
                        axios.post("/user/add.do?roleIds=" + this.roleIds, this.formData).then(response => {
                            if (response.data.flag) {
                                //隐藏弹出框
                                this.dialogFormVisible = false;
                                this.$message.success(response.data.message);
                                return;
                            }
                            this.$message.error(response.data.message);
                        }).catch((error) => {
                            this.showMessage(error);
                        }).finally(() => {
                            //重新查询分页信息
                            this.findPage();
                        })
                    } else {
                        //表单校验不通过
                    }
                });
            },
            //分页查询
            findPage() {
                var param = {
                    pageSize: this.pagination.pageSize,
                    currentPage: this.pagination.currentPage,
                    queryString: this.pagination.queryString
                };
                axios.post("/user/findPage.do", param).then((res) => {
                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows;
                }).catch((error) => {
                    this.showMessage(error);
                });
            },
            // 重置表单
            resetForm() {
                this.formData = {};
            },
            // 弹出添加窗口
            handleCreate() {
                //重置表单数据
                this.resetForm();
                //默认切换到第一个标签页（基本信息）
                this.activeName = 'first';
                //展示新增弹出框
                this.dialogFormVisible = true;
                //重置用户勾选的角色项复选框
                this.roleIds = [];
                axios.get("/role/findAll.do").then(resp => {
                    //查询检查项添加到
                    if (resp.data.flag) {
                        this.tableData = resp.data.data;
                    } else {
                        this.$message.error(resp.data.message);
                    }
                }).catch((error) => {
                    this.showMessage(error);
                });
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                this.resetForm();
                this.activeName = "first";
                this.formData = this.deepClone(row);//深拷贝完成操作
                this.formData.password = "";
                //查询当前用户基本信息
                // axios.get("/user/findByUserId.do?id=" + row.id).then(response => {
                //     if (!response.data.flag) {
                //         this.$message.error(response.data.message);
                //         return;
                //     }
                //     this.formData = response.data.data;
                //     this.formData.password = "";
                    //查询所有角色信息
                    axios.get("/role/findAll.do").then(response => {
                        if (!response.data.flag) {
                            this.$message.error(response.data.message);
                            return;
                        }
                        this.tableData = response.data.data;
                    });

                    //查询用户关联的角色项信息
                    axios.get("/user/findRoleByUserId.do?id=" + row.id).then(response => {
                        if (!response.data.flag) {
                            this.$message.error(response.data.message);
                            return;
                        }
                        this.roleIds = response.data.data;
                    });
            //     }).catch((error) => {
            //     this.showMessage(error);
            // });
                this.dialogFormVisible4Edit = true;
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            // 删除
            handleDelete(row) {
                this.$confirm("您确定要删除此记录吗?", "提示", {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //发送异步请求, 删除用户
                    axios.get("/user/delete.do?id=" + row.id).then(response => {
                        if (response.data.flag) {
                            this.$message.success(response.data.message);
                            return;
                        }
                        this.$message.error(response.data.message);
                    }).catch((error) => {
                        this.showMessage(error);
                    }).finally(() => {
                        this.findPage();
                    });
                });
            },
            //深拷贝
            deepClone(obj){
                return JSON.parse(JSON.stringify(obj));
            },
            showMessage(r) {
                //引入组件库
                if (r == 'Error: Request failed with status code 403') {
                    //权限不足
                    this.$message.error('无访问权限');
                    return;
                } else {
                    this.$message.error('未知错误');
                    return;
                }
            },
        }
    })
</script>
</html>